//
//  CertKeyUsage.m
//  TestProject
//
//  Created by 武侠 on 2022/2/17.
//  Copyright © 2022 zhulong. All rights reserved.
//

#import "CertKeyUsage.h"
#import "CertTLV.h"
#import "CertItemLength.h"
#import "_CertBitString.h"

@implementation CertKeyUsage

/*
 30 0E(14):
     06 03(03): 55 1D 0F     (2.5.29.15)
     01 01(01): FF           (true)
     04 04(04):
         03 02(02): 06 C0    (06c0)
 */
- (void)loadCertData {
    [super loadCertData];
    
    self.decipherOnly     = [self bit:15];
    self.encipherOnly     = [self bit:0];
    self.cRLSign          = [self bit:1];
    self.keyCertSign      = [self bit:2];
    self.keyAgreement     = [self bit:3];
    self.dataEncipherment = [self bit:4];
    self.keyEncipherment  = [self bit:5];
    self.nonRepudiation   = [self bit:6];
    self.digitalSignature = [self bit:7];
}

- (NSString *)description {
    NSMutableArray *array = [NSMutableArray array];
    if (self.digitalSignature == YES) {
        [array addObject:@"DigitalSignature(数字签名)"];
    }
    if (self.nonRepudiation == YES) {
        [array addObject:@"NonRepudiation(认可签名)"];
    }
    if (self.keyEncipherment == YES) {
        [array addObject:@"KeyEncipherment(密钥加密)"];
    }
    if (self.dataEncipherment == YES) {
        [array addObject:@"DataEncipherment(数据加密)"];
    }
    if (self.keyAgreement == YES) {
        [array addObject:@"KeyAgreement(密钥协商)"];
    }
    if (self.keyCertSign == YES) {
        [array addObject:@"KeyCertSign(证书签名)"];
    }
    if (self.cRLSign == YES) {
        [array addObject:@"CRLSign(CRL 签名)"];
    }
    if (self.encipherOnly == YES) {
        [array addObject:@"EncipherOnly(仅加密)"];
    }
    if (self.decipherOnly == YES) {
        [array addObject:@"DecipherOnly(仅解密)"];
    }
    if (array.count > 0) {
        NSMutableString *str = [NSMutableString stringWithString:@"["];
        [str appendString:[array componentsJoinedByString:@", "]];
        [str appendString:@"]"];
        return str;
    }
    return [super description];
}

@end
